<template>
  <MesBox
    title="业务排单"
    v-model="showSendModal"
    :lazy="true"
    :width="1300"
    :height="600"
    :onModelClose="cancel"
  >
    <MesForm
      :formRules="BusinessFormRules"
      :formFields="BusinessFormFields"
    ></MesForm>
    <MesHeader title="待生成计划列表">
      <template #default>
        <el-button size="small" link class="textColor" @click="addRow"
          ><i class="el-icon-plus" style="margin-right: 5px"></i
          >添加行</el-button
        >
        <el-button size="small" link class="textColor" @click="delRow"
          ><i class="el-icon-minus" style="margin-right: 5px"></i
          >删除行</el-button
        >
      </template>
    </MesHeader>
    <MesTable
      ref="planList"
      :columns="sendFormColumns"
      :loadKey="true"
      :clickEdit="true"
      :tableData="tableData"
      :index="true"
    ></MesTable>
    <template #footer>
      <el-button type="success" @click="saveData">
        <i class="el-icon-success" style="margin-right: 5px"></i>
        保存</el-button
      >
      <el-button type="danger" @click="cancel">
        <i class="el-icon-error" style="margin-right: 5px"></i>
        取消</el-button
      >
    </template>
  </MesBox>
</template>

<script setup>
import MesBox from '@/components/basic/MesBox'
import MesForm from '@/components/basic/MesForm'
import MesHeader from '@/components/basic/MesHeader'
import MesTable from '@/components/basic/MesTable'
import { getNowDate } from '@/uitils/dateFormatUtil'
import { getCurrentInstance, ref } from 'vue'

const planList = ref(null)
const emit = defineEmits(['parentCall'])
const that = getCurrentInstance().appContext.config.globalProperties
const tableData = ref([])
const showSendModal = ref(false)
const BusinessFormRules = [
  [
    {
      title: '产品名称',
      field: 'ProductName',
      disabled: true,
    },
    {
      title: '产品编码',
      field: 'ProductCode',
      disabled: true,
    },
    {
      title: '产品规格',
      field: 'ProductStandard',
      disabled: true,
    },
  ],
  [
    {
      title: '客户编码',
      field: 'CusCode',
      disabled: true,
    },
    {
      title: '客户名称',
      field: 'CusName',
      disabled: true,
    },
    {
      title: '订单号',
      field: 'MoCode',
      disabled: true,
    },
  ],
  [
    {
      title: '订单数量',
      field: 'Qty',
      disabled: true,
    },
    {
      title: '计划开始时间',
      field: 'PlanStartDate',
      disabled: true,
    },
    {
      title: '计划结束时间',
      field: 'PlanEndDate',
      disabled: true,
    },
  ],
  [
    {
      title: 'SalesOrder_Id',
      field: 'SalesOrder_Id',
      hidden: true,
    },
  ],
]

const BusinessFormFields = ref({
  ProductName: '',
  ProductCode: '',
  ProductStandard: '',
  CusCode: '',
  CusName: '',
  MoCode: '',
  Qty: 0,
  PlanStartDate: '',
  PlanEndDate: '',
  SalesOrder_Id: '',
})
const sendFormColumns = [
  {
    title: '计划数量',
    field: 'qty',
    required: true,
    min: 0,
    edit: {
      type: 'number',
    },
  },
  {
    title: '计划开始时间',
    field: 'planStartDate',
    required: true,
    edit: {
      type: 'datetime',
    },
  },
  {
    title: '计划结束时间',
    field: 'planEndDate',
    required: true,
    edit: {
      type: 'datetime',
    },
  },
  {
    title: '交付日期',
    field: 'deliveryDate',
    required: true,
    edit: {
      type: 'datetime',
    },
  },
]

//保存
const saveData = async () => {
  if (tableData.value.length) {
    const findSelect = tableData.value.find(
      (item) =>
        !item.qty ||
        !item.planStartDate ||
        !item.planEndDate ||
        !item.deliveryDate
    )
    if (findSelect == undefined) {
      const id = BusinessFormFields.value.SalesOrder_Id
      const childrenlist = tableData.value
      const res = await that.http.post(
        '/api/Production_WorkOrder/CreateChildRenOrder11',
        { id, childrenlist },
        true
      )
      if (res.status) {
        that.$message.success('创建计划成功')
        cancel()
        emit('parentCall', ($parent) => {
          $parent.search()
        })
      } else {
        that.$message.error(res.message)
      }
    } else {
      that.$message.error('请填写完整信息')
    }
  } else {
    that.$message.error('至少添加一项计划才能生成')
  }
}

//取消
const cancel = () => {
  showSendModal.value = false
  planList.value.reset() //表单重置
  tableData.value = []
}

const addRow = () => {
  tableData.value.push({
    qty: 0,
    planStartDate: getNowDate(),
    planEndDate: '',
    deliveryDate: '',
  })
}

const delRow = () => {
  planList.value.delRow()
}

const open = (row) => {
  showSendModal.value = true
  for (const key in BusinessFormFields.value) {
    BusinessFormFields.value[key] = row[key]
  }
}

defineExpose({
  open,
})
</script>
